<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use Moell\Mojito\Http\Controllers\PermissionGroupController as MojitoPermissionGroupController;
use Moell\Mojito\Models\PermissionGroup;

class PermissionGroupController extends MojitoPermissionGroupController
{
    public function guardNameAndSubForPermissions($guardName)
    {
        $permissionIds = Auth::user()->getAllPermissions()->pluck('id')->toArray();
        $permissionGroups = PermissionGroup::query()
            ->with(['permission' => function ($query) use ($guardName, $permissionIds) {
                $query->where('guard_name', $guardName)->whereIn('id', $permissionIds);
            }])
            ->get()->filter(function($item)  {
                return count($item->permission) > 0;
            });
        $permissionGroups = $permissionGroups->toArray();
        return response()->json([
            'data' => array_values($permissionGroups)
        ]);
    }
}
